<template>
  <div class="app-container">
    <div>
      <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect">
        <el-menu-item v-for="(item,index) in agreementList" :index="String(index)">{{item.agreementName}}</el-menu-item>
      </el-menu>
    </div>
    <div style="width: 100%;background-color: #FFFFFF;display: flex;justify-content: center;padding: 40px 200px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="协议内容">
          <tinymce v-if="open" v-model="form.content" style="" :height="500"
                   :img-base-url="imgUrl"
          />
        </el-form-item>
        <el-form-item label="协议状态" prop="agreementStatus">
          <el-switch
            v-model="status_switch"
            active-color="#13ce66"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <div style="display: flex;justify-content: center">
          <el-button type="primary" @click="submitForm">保存</el-button>
        </div>
      </el-form>
    </div>
<!--    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">-->
<!--      <el-form-item label="协议名" prop="agreementName">-->
<!--        <el-input-->
<!--          v-model="queryParams.agreementName"-->
<!--          placeholder="请输入协议名"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item>-->
<!--        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
<!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
<!--      </el-form-item>-->
<!--    </el-form>-->

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['user:agreement:add']"-->
<!--        >新增-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['user:agreement:remove']"-->
<!--        >删除-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--&lt;!&ndash;      <el-col :span="1.5">&ndash;&gt;-->
<!--&lt;!&ndash;        <el-button&ndash;&gt;-->
<!--&lt;!&ndash;          type="warning"&ndash;&gt;-->
<!--&lt;!&ndash;          plain&ndash;&gt;-->
<!--&lt;!&ndash;          icon="el-icon-download"&ndash;&gt;-->
<!--&lt;!&ndash;          size="mini"&ndash;&gt;-->
<!--&lt;!&ndash;          @click="handleExport"&ndash;&gt;-->
<!--&lt;!&ndash;          v-hasPermi="['user:agreement:export']"&ndash;&gt;-->
<!--&lt;!&ndash;        >导出&ndash;&gt;-->
<!--&lt;!&ndash;        </el-button>&ndash;&gt;-->
<!--&lt;!&ndash;      </el-col>&ndash;&gt;-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

<!--    <el-table v-loading="loading" :data="agreementList" @selection-change="handleSelectionChange">-->
<!--      <el-table-column type="selection" width="55" align="center"/>-->
<!--      <el-table-column type="index" width="50"></el-table-column>-->
<!--      <el-table-column label="协议名" align="center" prop="agreementName"/>-->
<!--      <el-table-column label="协议状态" align="center" prop="agreementStatus">-->
<!--        &lt;!&ndash;        （0：不使用，1：使用）&ndash;&gt;-->
<!--        <template slot-scope="scope">-->
<!--          <el-switch-->
<!--            v-model="scope.row.agreementStatus==1?true:false"-->
<!--            active-color="#13ce66"-->
<!--            @change="agreement_change(scope.$index)"-->
<!--          >-->
<!--          </el-switch>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="备注" align="center" prop="remark"/>-->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['user:agreement:edit']"-->
<!--          >修改-->
<!--          </el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['user:agreement:remove']"-->
<!--          >删除-->
<!--          </el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--    </el-table>-->

<!--    <pagination-->
<!--      v-show="total>0"-->
<!--      :total="total"-->
<!--      :page.sync="queryParams.pageNum"-->
<!--      :limit.sync="queryParams.pageSize"-->
<!--      @pagination="getList"-->
<!--    />-->

    <!-- 添加或修改协议列表对话框 -->
<!--    <el-dialog :title="title" :visible.sync="open" width="950px" append-to-body>-->
<!--      <el-form ref="form" :model="form" :rules="rules" label-width="80px">-->
<!--        <el-form-item label="协议名" prop="agreementName">-->
<!--          <el-input v-model="form.agreementName" placeholder="请输入协议名"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="协议内容">-->
<!--          &lt;!&ndash;          <editor v-model="form.content" :min-height="192"/>&ndash;&gt;-->
<!--          <tinymce v-if="open" v-model="form.content" style="" :height="500"-->
<!--                   :img-base-url="imgUrl"-->
<!--          />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="协议状态" prop="agreementStatus">-->
<!--          <el-switch-->
<!--            v-model="status_switch"-->
<!--            active-color="#13ce66"-->
<!--          >-->
<!--          </el-switch>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备注" prop="remark">-->
<!--          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
<!--        <el-button @click="cancel">取 消</el-button>-->
<!--      </div>-->
<!--    </el-dialog>-->
  </div>
</template>

<script>
import { listAgreement, getAgreement, delAgreement, addAgreement, updateAgreement } from '@/api/user/agreement'
import Tinymce from '@/components/Tinymce'
import { updateDonate } from '@/api/donate/donate'
import { imgUrl } from '@/utils/request'

export default {
  name: 'Agreement',
  components: {
    Tinymce
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 协议列表表格数据
      agreementList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 菜单
      activeIndex: '0',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        agreementName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 使用状态
      status_switch: false,
      imgUrl,
      request_url: process.env.VUE_APP_BASE_API
    }

  },
  created() {
    this.getList()
  },
  methods: {
    //使用状态切换
    handleSelect(key, keyPath) {
      console.log(key, keyPath);
      this.activeIndex = String(key)
      this.open = false
      this.handleUpdate(this.agreementList[Number(this.activeIndex)].id)
    },
    //使用状态切换
    agreement_change(index) {
      this.agreementList[index].agreementStatus == 0 ? this.agreementList[index].agreementStatus = '1' : this.agreementList[index].agreementStatus = '0'
      updateAgreement(this.agreementList[index]).then(response => {
        this.$modal.msgSuccess('修改成功')
        this.getList()
      })
    },
    /** 查询协议列表列表 */
    getList() {
      this.loading = true
      listAgreement(this.queryParams).then(response => {
        this.agreementList = response.pageData
        this.total = response.total
        this.loading = false
        this.handleUpdate(this.agreementList[Number(this.activeIndex)].id)
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        agreementName: null,
        content: null,
        agreementStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加协议列表'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row
      getAgreement(id).then(response => {
        this.form = response
        this.open = true
        this.form.agreementStatus == 1 ? this.status_switch = true : this.status_switch = false
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          console.log(this.form.content)
          this.status_switch ? this.form.agreementStatus = '1' : this.form.agreementStatus = '0'
          if (this.form.id != null) {
            updateAgreement(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除协议列表编号为"' + ids + '"的数据项？').then(function() {
        return delAgreement(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('user/agreement/export', {
        ...this.queryParams
      }, `agreement_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
